'use strict';

import {
    HttpService,
    Tips
} from './index'
import $ from 'jquery'

export default class wxServer {

    constructor() {}

    static createNonceStr() {
        return Math.random().toString(36).substr(2, 15);
    }

    static createTimeStamp() {
        return parseInt(new Date().getTime() / 1000) + '';
    }

    static wx(noncestr, timestamp) {
        if (!/micromessenger/.test(navigator.userAgent.toLowerCase()))
            return
        $.ajax({
            url: 'http://api.17find.com/thirdparty/weixin/getsignature?'+`url=${encodeURIComponent(location.href.split('#')[0])}&noncestr=${noncestr}&timestamp=${timestamp}&weixinclient=${true}`,
            type: 'POST',
            success: function(res) {
                if (res.code == 50006) {
                    sessionStorage.wxinit = 1
                    wx.config({
                        debug: false,
                        appId: res.data.appid,
                        timestamp: res.data.timestamp,
                        nonceStr: res.data.noncestr,
                        signature: res.data.signature,
                        jsApiList: [
                            'chooseImage',
                            'uploadImage',
                            'chooseWXPay',
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'scanQRCode',
                            'startRecord',
                            'stopRecord',
                            'onVoiceRecordEnd',
                            'playVoice',
                            'pauseVoice',
                            'stopVoice',
                            'onVoicePlayEnd',
                            'uploadVoice',
                            'downloadVoice'
                        ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                    });
                } else {
                    wx.error(function(res) {
                        Tips('error')
                        // config信息验证失败会执行error函数
                    });
                }
            }
        })
        // HttpService.query({
        //     url: '/thirdparty/weixin/getsignature',
        //     data: {
        //         url: location.href.split('#')[0],
        //         noncestr: noncestr,
        //         timestamp: timestamp,
        //         weixinclient: 'true'
        //     },
        //     success: (res => {
        //         if (res.code == 50006) {
        //             wx.config({
        //                 debug: false,
        //                 appId: res.data.appid,
        //                 timestamp: res.data.timestamp,
        //                 nonceStr: res.data.noncestr,
        //                 signature: res.data.signature,
        //                 jsApiList: [
        //                     'chooseImage',
        //                     'uploadImage',
        //                     'chooseWXPay',
        //                     'onMenuShareTimeline',
        //                     'onMenuShareAppMessage',
        //                     'scanQRCode',
        //                     'startRecord',
        //                     'stopRecord',
        //                     'onVoiceRecordEnd',
        //                     'playVoice',
        //                     'pauseVoice',
        //                     'stopVoice',
        //                     'onVoicePlayEnd',
        //                     'uploadVoice',
        //                     'downloadVoice'
        //                 ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        //             });
        //         } else {
        //             wx.error(function(res) {
        //                 Tips('error')
        //                 // config信息验证失败会执行error函数
        //             });
        //         }
        //     })
        // });

    }

    static init() {
        const nonceStr = this.createNonceStr();
        const timestamp = this.createTimeStamp();
        this.wx(nonceStr, timestamp)
    }
}
